package com.mycompany.ui.panel.searches;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.mycompany.model.Provincia;
import com.mycompany.model.Regione;
import com.mycompany.model.User;
import com.mycompany.queryBuilder.QueryBuilder;
import com.mycompany.repositories.RegioneRepository;
import com.mycompany.repositories.UserRepository;
import com.mysema.query.types.Predicate;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;

public class ClientiSearchPanel extends GenericSearchPanel{
	
	@Autowired
	UserRepository userRepository;
	
	@Autowired
	RegioneRepository regioneRepository;
	
	private ComboBox comboBoxRegione = new ComboBox("Regione");
	private ComboBox comboBoxProvincia = new ComboBox("Provincia");

	public ClientiSearchPanel() {
		
		super();
		
		
		reInitField();
		initComboBoxes();
		reInitTable();
		addListeners();	
		
		gridLayout = createGridLayout();
		verticalLayout.addComponent(gridLayout);
		verticalLayout.setComponentAlignment(gridLayout,Alignment.MIDDLE_CENTER);
		verticalLayout.addComponent(tabellaRisultati);
		verticalLayout.setComponentAlignment(tabellaRisultati,Alignment.MIDDLE_CENTER);
		setContent(verticalLayout);
	}

	private void initComboBoxes() {
		// TODO Auto-generated method stub
		List<Regione> regioni = regioneRepository.findAll();
		
		for(Regione regione : regioneRepository.findAll())
			comboBoxRegione.addItem(regione);
		
	
		for(Provincia provincia :  regioni.get(0).getProvincie())
			comboBoxProvincia.addItem(provincia);
			
		comboBoxProvincia.setWidth("140px");
		comboBoxRegione.setWidth("140px");
		
	}

	private void reInitField() {
		// TODO Auto-generated method stub
		field1.setCaption("Nome");
		field2.setCaption("Cognome");
		field3.setCaption("CodiceFiscale");
		field4.setCaption("Email");
		field5.setCaption("Comune");
		
	}

	private void reInitTable() {
		// TODO Auto-generated method stub
		beanItemContainer = new BeanItemContainer<User>(User.class);
		beanItemContainer.addNestedContainerProperty("indirizzo.regione");
		beanItemContainer.addNestedContainerProperty("indirizzo.provincia");
		tabellaRisultati.setContainerDataSource(beanItemContainer);

		tabellaRisultati.setCaption("Risultati Cliente");
		tabellaRisultati.setVisibleColumns(new Object [] {"nome", "cognome", "codiceFiscale", "email","indirizzo.provincia","indirizzo.regione"});
        tabellaRisultati.setColumnHeaders(new String [] {"nome", "cognome", "codiceFiscale", "email","provincia","regione"});
		
	}

	private void fillContainer(Predicate predicate) {
		beanItemContainer.removeAllItems();
		// TODO Auto-generated method stub
		for(User user : userRepository.findAll(predicate))
			beanItemContainer.addBean(user);
		
	}

	private void addListeners() {
		// TODO Auto-generated method stub
		button.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				
				Regione regioneScelta = (Regione) comboBoxRegione.getValue();
				Provincia provinciaScelta = (Provincia) comboBoxProvincia.getValue();
				
				if(regioneScelta == null)
					regioneScelta = new Regione();
				if(provinciaScelta == null)
					provinciaScelta = new Provincia();
				
				String [] valueComponents = {field1.getValue(),field2.getValue(),field3.getValue(),
											field4.getValue(),field5.getValue(),regioneScelta.getName(),
											provinciaScelta.getNome()};
								
				Predicate clientiDaCercare = QueryBuilder.buildQuery(valueComponents);
				fillContainer(clientiDaCercare);
				
			}
		});
	}


	protected GridLayout createGridLayout() {
		
		GridLayout gridLayout = new GridLayout(2,4);
		gridLayout.setWidth("50%");
		gridLayout.addComponent(field1);
		gridLayout.addComponent(field2);
		gridLayout.addComponent(field3);
		gridLayout.addComponent(field4);
		gridLayout.addComponent(field5);
		gridLayout.addComponent(comboBoxRegione);
		gridLayout.addComponent(comboBoxProvincia);
		gridLayout.addComponent(button);
		gridLayout.setComponentAlignment(button, Alignment.BOTTOM_LEFT);
		gridLayout.setMargin(true);
		
		return gridLayout;
	}

	

	
	

}
